package com.idealighter.game.dictionary.domwrapper;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;

import com.google.common.collect.Range;
import com.google.common.collect.RangeMap;
import com.google.common.collect.TreeRangeMap;

import com.idealighter.game.dictionary.domain.DoudizhuMatchRoomDomain;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * DoudizhuMatchRoomDomainWrapper .
 * 
 * 
 * @date 2016年2月29日 下午5:52:08
 *
 */
public class DoudizhuMatchRoomDomWrapper extends DoudizhuMatchRoomDomain {

  private final DoudizhuMatchRoomDomain dom;

  private final List<Integer> finalsAdvanceNumsData;
  private final RangeMap<Integer, List<Reward>> rewardsData = TreeRangeMap.create();

  /**
   * 构造函数.
   * 
   * @param dom 房间数据.
   */
  public DoudizhuMatchRoomDomWrapper(DoudizhuMatchRoomDomain dom) {
    this.dom = dom;
    this.finalsAdvanceNumsData =
        JSONArray.parseArray("[" + dom.getFinalsAdvanceNums() + "]", Integer.class);
    // 奖励配置
    JSONObject reward = JSON.parseObject(dom.getReward(), Feature.OrderedField);

    int preRank = 0;
    for (String rankStr : reward.keySet()) {
      int rank = Integer.parseInt(rankStr);
      rewardsData.put(Range.openClosed(preRank, rank), rewards(reward.getJSONObject(rankStr)));
      preRank = rank;
    }
  }

  /**
   * 解析奖励 .
   * 
   * @param rewardsJson . .
   * @return .
   */
  private List<Reward> rewards(JSONObject rewardsJson) {
    List<Reward> rewards = new ArrayList<>();

    for (String item : rewardsJson.keySet()) {
      rewards.add(new Reward(Integer.valueOf(item), rewardsJson.getIntValue(item)));
    }
    return rewards;
  }

  /**
   * 根据排名获取奖励 .
   * 
   * @param rank . .
   * @return .
   */
  public List<Reward> getRewards(int rank) {
    return rewardsData.get(rank);
  }

  /**
   * 获取决赛runs的晋级人数 .
   * 
   * @param runs . .
   * @return .
   */
  public int getFinalsAdvanceNum(int runs) {

    return finalsAdvanceNumsData.get(runs - 1);
  }

  /**
   * getFinalsAdvanceNumsData .
   * 
   * @return .
   */
  public List<Integer> getFinalsAdvanceNumsData() {
    return finalsAdvanceNumsData;
  }

  public int getId() {
    return dom.getId();
  }

  public void setId(int id) {
    dom.setId(id);
  }

  public String getName() {
    return dom.getName();
  }

  public void setName(String name) {
    dom.setName(name);
  }

  public int getType() {
    return dom.getType();
  }

  public void setType(int type) {
    dom.setType(type);
  }

  public int getNum() {
    return dom.getNum();
  }

  public void setNum(int num) {
    dom.setNum(num);
  }

  public String getCron() {
    return dom.getCron();
  }

  public void setCron(String cron) {
    dom.setCron(cron);
  }

  public int getInitCedits() {
    return dom.getInitCedits();
  }

  public void setInitCedits(int initCedits) {
    dom.setInitCedits(initCedits);
  }

  public int getPreEndNum() {
    return dom.getPreEndNum();
  }

  public void setPreEndNum(int preAdvanceNum) {
    dom.setPreEndNum(preAdvanceNum);
  }

  public int getPreChangeTime() {
    return dom.getPreChangeTime();
  }

  public void setPreChangeTime(int preChangeTime) {
    dom.setPreChangeTime(preChangeTime);
  }

  public int getPreChangeCedits() {
    return dom.getPreChangeCedits();
  }

  public void setPreChangeCedits(int preChangeCedits) {
    dom.setPreChangeCedits(preChangeCedits);
  }

  public int getFinalsRuns() {
    return dom.getFinalsRuns();
  }

  public void setFinalsRuns(int finalsRuns) {
    dom.setFinalsRuns(finalsRuns);
  }

  public String getFinalsAdvanceNums() {
    return dom.getFinalsAdvanceNums();
  }

  public void setFinalsAdvanceNums(String finalsAdvanceNums) {
    dom.setFinalsAdvanceNums(finalsAdvanceNums);
  }

  public int getFinalsCedits() {
    return dom.getFinalsCedits();
  }

  public void setFinalsCedits(int finalsCedits) {
    dom.setFinalsCedits(finalsCedits);
  }

  public int getFinalsRunsTimes() {
    return dom.getFinalsRunsTimes();
  }

  public void setFinalsRunsTimes(int finalsRunsTimes) {
    dom.setFinalsRunsTimes(finalsRunsTimes);
  }

  public int getGold() {
    return dom.getGold();
  }

  public void setGold(int gold) {
    dom.setGold(gold);
  }

  public int getTickets() {
    return dom.getTickets();
  }

  public void setTickets(int tickets) {
    dom.setTickets(tickets);
  }

  public byte getIsActive() {
    return dom.getIsActive();
  }

  public void setIsActive(byte isActive) {
    dom.setIsActive(isActive);
  }

  public Date getTimeCreate() {
    return dom.getTimeCreate();
  }

  public void setTimeCreate(Date timeCreate) {
    dom.setTimeCreate(timeCreate);
  }

  public Date getTimeOpen() {
    return dom.getTimeOpen();
  }

  public void setTimeOpen(Date timeOpen) {
    dom.setTimeOpen(timeOpen);
  }

  public int getPreCedits() {
    return dom.getPreCedits();
  }

  public void setPreCedits(int preCedits) {
    dom.setPreCedits(preCedits);
  }

  /**
   * 奖励 .
   * 
   * 
   * @date 2016年3月4日 下午6:17:24
   *
   */
  public static class Reward {
    // 奖励的道具id
    private int id;
    // 奖励的道具的数量
    private int num;

    public Reward() {}

    /**
     * 构造函数.
     * 
     * @param id . .
     * @param num . .
     */
    public Reward(int id, int num) {
      this.id = id;
      this.num = num;
    }

    public int getId() {
      return id;
    }

    public void setId(int id) {
      this.id = id;
    }

    public int getNum() {
      return num;
    }

    public void setNum(int num) {
      this.num = num;
    }

  }

  public String getReward() {
    return dom.getReward();
  }

  public void setReward(String reward) {
    dom.setReward(reward);
  }

}
